<?php
namespace app\common\model;

use QL\QueryList;

class CollectProjectModel extends BaseModel
{
    protected $name = 'collect_project'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改
    //类型自动转换
    protected $type = [
        'collect_url_rule' => 'array',
    ];

    //内容url采集
    public function testCollectList($data){
        $url = $data['test_list_url'];
        $collect = $data['collect_url_rule'];
        $rules = [
            'title' => [html_out($collect['title']['key']),html_out($collect['title']['value'])],
            'url' => [html_out($collect['url']['key']),html_out($collect['url']['value'])],
        ];
        $range = html_out($collect['range']);
        $inputEncoding = $data['input_encoding'];
        $outputEncoding = $data['output_encoding'];
        $result = QueryList::Query($url,$rules,$range,$outputEncoding,$inputEncoding,true)->data;
        foreach ($result as $k=>$v){
            if(strstr($v['url'],$data['project_url'])===false){ //未找到前缀
                $result[$k]['url'] = $data['project_url'].$v['url'];
            }
        }
        return $result;
    }
    //内容页采集
    public function testCollectContent($data){
        $url = $data['test_content_url'];
        $collect = json_decode(html_out($data['collect_content_rule']),true);
        $inputEncoding = $data['input_encoding'];
        $outputEncoding = $data['output_encoding'];
        $result = QueryList::Query($url,$collect['rules'],$collect['range'],$outputEncoding,$inputEncoding,true)->data;
        if($result){
            return $result[0];
        }
        return [];
    }
}